package com.example.sharedpreferencestest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.sharedpreferencestest.base.BaseActivity;

public class MainActivity extends BaseActivity {

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 读取数据
        Button restore_data = findViewById(R.id.restore_data);
        restore_data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences pref = getSharedPreferences("data", MODE_PRIVATE);
                String name = pref.getString("name", "");
                int age = pref.getInt("age", 0);
                boolean married = pref.getBoolean("married", false);
                Log.d(TAG, "name is : " + name);
                Log.d(TAG, "age is : " + age);
                Log.d(TAG, "married is : " + married);
            }
        });


//        Button save_data = findViewById(R.id.save_data);
//        // 点击按钮，保存数据
//        save_data.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                // 使用第三种方式添加数据
//                SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
//                editor.putString("name", "刘德华");
//                editor.putInt("age", 45);
//                editor.putBoolean("married", true);
//                editor.apply();
//            }
//        });

    }
}
